<?php
namespace Classes;

class Paginator {  
    public $items_total; 
    public $current_page; 
    public $items_per_page;  
    public $limit_start;
    public $nb_pages;
    public $first_text = '<<';
    public $prev_text = '<';
    public $next_text = '>';
    public $last_text = '>>';
    public $page_nav_class = '';
    public $page_nav_desabled_class = '';
    public $start_num;
    
    public function __construct($items_total, $default_items_per_page=25) {
        $this->items_total = (int) $items_total;
        $this->current_page = $this->currentPage(1);
        $this->items_per_page = $this->itemsPerPage($default_items_per_page);
        $this->limit_start = $this->limitStart();
        $this->nb_pages = $this->nbPages();
        $this->start_num = ($this->current_page * $this->items_per_page) - ($this->items_per_page - 1);
    }
    
    /**
     * Retourne le numéro de page courant
     * @param int $default : Numéro de page par défaut
     * @return int $current_page
     */
    public function currentPage($default) {
        $current_page = $default;
        if(isset($_POST['current_page'])) {
            $current_page = $_POST['current_page'];
        }        
        return $current_page;
    }
    
    /**
     * Retourne le nombre d'éléments par page choisi
     * @param int $default : Nombre d'éléments par page par défaut
     * @return int $item_pet_page
     */
    public function itemsPerPage($default) {
        $items_per_page = $default;
        if(isset($_POST['items_per_page'])) {
            $items_per_page = $_POST['items_per_page'];
        }
        if($items_per_page == 0) {
            $items_per_page = null;
        }
        return $items_per_page;
    }
    
    /**
     * Retourne la position à partir de lequel on souhaite afficher les enregistrements
     * @return int $limit_start
     */
    public function limitStart() {        
        if($this->items_per_page == 0) {
            $limit_start = null;
        } else {
            $limit_start = ($this->current_page - 1) * $this->items_per_page;
        }
        return $limit_start;
    }
    
    /**
     * Retourne le nombre de page total
     * @return int $nb_pages
     */
    public function nbPages() {
        if($this->items_per_page == 0) {
            $nb_pages = 1;
        } else {
            $nb_pages = ceil($this->items_total / $this->items_per_page);
        }
        return $nb_pages;
    }
    
    /**
     * Selecte la valeur choisie dans le choix du nombre d'éléments par page
     * @param type $value : Valeur choisie
     * @return string $selected
     */
    public function echoSelected($value) {
        $selected = '';
        if(isset($_POST['items_per_page'])) {
            $val = $_POST['items_per_page'];
        } else {
            $val = $this->items_per_page;
        }
        
        if($val == $value) {
            $selected = 'selected="selected"';
        }
        return $selected;
    }
    
    /**
     * Retourne la possibilité de choisir le nombre d'éléments par page
     * @return string $output
     */
    public function displayPagerSelector() {
        $output = '<select name="items_per_page" onchange="submitForm()">'
                . '<option value="10" '.$this->echoSelected(10).'>10</option>'
                . '<option value="25" '.$this->echoSelected(25).'>25</option>'
                . '<option value="50" '.$this->echoSelected(50).'>50</option>'
                . '<option value="100" '.$this->echoSelected(100).'>100</option>'
                . '<option value="0" '.$this->echoSelected(0).'>Tout</option>'
                . '</select>&nbsp;&nbsp;&nbsp;';
        return $output;
    }
    
    /**
     * Retourne les informations sur la naviguation
     * @return string $output
     */
    public function displayPagerInfos() {
        $output = '&nbsp;&nbsp;Page '.$this->current_page.' sur '.$this->nb_pages;
        return $output;
    }
    
    /**
     * Retourne les pages de naviguation avec leur liens
     * @return string $output
     */
    public function displayPagerLinks() {
        $output = ''; 
        
        // Affiche Début/Précédent
        if($this->nb_pages > 1) {
            if ($this->current_page == 1) {
                $output .= '<a class="'.$this->page_nav_desabled_class.'">'.$this->first_text.'</a> ';
                $output .= '<a class="'.$this->page_nav_desabled_class.'">'.$this->prev_text.'</a> ';
            } else {
                $output .= '<a class="'.$this->page_nav_class.'" onclick="currentPage(1)">'.$this->first_text.'</a> ';
                $output .= '<a class="'.$this->page_nav_class.'" onclick="previousPage('.$this->current_page.')">'.$this->prev_text.'</a> ';
            }
        }
        
        // Affiche les pages disponibles
        for ($i = 1 ; $i <= $this->nb_pages ; $i++) {
            if ($i == $this->current_page) {
                $output .= '<a class="'.$this->page_nav_desabled_class.'">'.$i.'</a> ';
            } else {
                $output .= '<a class="'.$this->page_nav_class.'" onclick="currentPage('.$i.')">'.$i.'</a> ';
            }
        }
        
        // Affiche Suivant/Fin
        if($this->nb_pages > 1) {
            if ($this->current_page == $this->nb_pages) {
                $output .= '<a class="'.$this->page_nav_desabled_class.'">'.$this->next_text.'</a> ';
                $output .= '<a class="'.$this->page_nav_desabled_class.'">'.$this->last_text.'</a> ';
            } else {
                $output .= '<a class="'.$this->page_nav_class.'" onclick="nextPage('.$this->current_page.')">'.$this->next_text.'</a> ';
                $output .= '<a class="'.$this->page_nav_class.'" onclick="currentPage('.$this->nb_pages.')">'.$this->last_text.'</a> ';
            }
        }
        
        return $output;
    }
    
    /**
     * Affiche le rendu de la pagination
     * @param type $showPagerSelector : Si on voudrai avoir la possibilité de choisir le nombre d'éléments par page
     * @param type $showPagerLinks : Si on voudrai afficher les pages de naviguation
     * @param type $showPagerInfos : Si on voudrai afficher les informations de naviguation
     * @return string $output
     */
    public function pager($showPagerSelector=true, $showPagerLinks=true, $showPagerInfos=true) {
        $output = '<script type="text/javascript">'
                . 'function submitForm() {'
                . 'document.forms["filter-form"].submit();'
                . '}'
                . 'function currentPage(page) {'
                . 'var currentPageInput = document.getElementById("current_page");'
                . 'currentPageInput.value = page;'
                . 'document.forms["filter-form"].submit();'
                . '}'
                . 'function previousPage(page) {'
                . 'var currentPageInput = document.getElementById("current_page");'
                . 'var current_page = page - 1;'
                . 'if(current_page == 0) {'
                . 'current_page = 1;'
                . '}'
                . 'currentPageInput.value = current_page;'
                . 'document.forms["filter-form"].submit();'
                . '}'
                . 'function nextPage(page) {'
                . 'var currentPageInput = document.getElementById("current_page");'
                . 'var current_page = page + 1;'
                . 'if(current_page == '.$this->nb_pages.') {'
                . 'current_page = '.$this->nb_pages.';'
                . '}'
                . 'currentPageInput.value = current_page;'
                . 'document.forms["filter-form"].submit();'
                . '}'
                . '</script>'
                . '<div id="paginator">'
                . '<table>'
                . '<tr>';
        if($showPagerSelector) { 
            $output.= '<td><div class="pagerSelector">'.$this->displayPagerSelector().'</div></td>';
        }
        if($showPagerLinks) { 
            $output.= '<td><div class="pagerLinks">'.$this->displayPagerLinks().'</div></td>';             
        }
        if($showPagerInfos) { 
            $output.= '<td><div class="pagerInfos">'.$this->displayPagerInfos().'</div></td>';         
        }
        $output .= '</tr>'
                . '</table>'
                . '<input type="hidden" id="current_page" name="current_page" value="1" />'
                . '</div>';
        return $output;
    }
    
    /**
     * Ouvre un formulaire
     * @return string $output
     */
    public function beginForm() {
        return $output = '<form method="post" id="filter-form">';
    }
    
    /**
     * Ferme un formulaire
     * @return string $ouptut
     */
    public function endForm() {
        return $output = '</form>';
    }
}  
?>  

